#include "RigidBody.h"

#include <vector>
#include <list>
#include "AABBTree.h"

namespace physicsworks {
	
RigidBody::RigidBody(BodyShape* bodyShape)
 : name("anonymous"), state(Dynamic), gravityCenter(vec3::ZERO), bodyShape(bodyShape)
{
	dynamicStates.push(new DynamicState);
}

RigidBody::~RigidBody()
{
	while (!dynamicStates.empty()) {
		delete dynamicStates.top();
		dynamicStates.pop();
	}
	removeCollisions();
}

/**
 * Remove all but the newest dynamic state.
 */
void RigidBody::removePastDynamicStates()
{
	DynamicState* newestDynamicState = dynamicStates.top();
	dynamicStates.pop();
	
	while (!dynamicStates.empty()) {
		delete dynamicStates.top();
		dynamicStates.pop();
	}
	
	dynamicStates.push(newestDynamicState);
}

/**
 * Remove all collisions.
 */
void RigidBody::removeCollisions()
{
	std::list<Collision*>::const_iterator iter;
	for (iter = collisions.begin(); iter != collisions.end(); ++iter)
		delete *iter;
	collisions.clear();
}

}
